package com.ivy.parser.utils;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.ivy.builder.graph.IvyCmp;
import com.ivy.builder.graph.NodeProperties;
import com.ivy.parser.bus.*;
import com.ivy.parser.execption.LiteFlowELException;
import com.yomahub.liteflow.builder.el.ELWrapper;
import com.yomahub.liteflow.enums.NodeTypeEnum;

public class NodeInfoToELUtil {
    public static ELWrapper buildELWrapper(NodeProperties nodeInfoWrapper) throws LiteFlowELException {
        return buildELWrapper(nodeInfoWrapper, false);
    }

    public static ELWrapper buildELWrapper(NodeProperties nodeInfoWrapper, boolean isSimple) throws LiteFlowELException {
        String componentId = nodeInfoWrapper.getComponentId();
        if (StrUtil.isBlank(componentId)) {
            throw new LiteFlowELException("节点组件ID为空");
        }
        return toELWrapper(nodeInfoWrapper, isSimple);
    }

    public static ELWrapper toELWrapper(IvyCmp info) {
        return toELWrapper(info, false);
    }

    public static ELWrapper toELWrapper(IvyCmp info, boolean isSimple) {
        handler(info);
        ELWrapper el = null;

        String nodeType = info.getNodeType();
        NodeTypeEnum enumByCode = NodeTypeEnum.getEnumByCode(nodeType);
        switch (enumByCode) {
            case COMMON:
                if (isSimple) {
                    el = ELBusNode.node(info);
                } else {
                    el = ELBusThen.node(info);
                }
                break;
            case SWITCH:
                el = ELBusSwitch.NEW().node(info).toELWrapper();
                break;
            case BOOLEAN:
                el = ELBusIf.NEW().node(info).toELWrapper();
                break;
            case FOR:
                el = ELBusFor.node(info);
                break;
            case ITERATOR:
                el = ELBusIterator.node(info);
                break;
            case SCRIPT:
                if (isSimple) {
                    el = ELBusNode.node(info);
                } else {
                    el = ELBusThen.node(info);
                }
                break;
            case SWITCH_SCRIPT:
                el = ELBusSwitch.NEW().node(info).toELWrapper();
                break;
            case BOOLEAN_SCRIPT:
                el = ELBusIf.NEW().node(info).toELWrapper();
                break;
            case FOR_SCRIPT:
                el = ELBusFor.node(info);
                break;
            case FALLBACK:
            default:
        }
        return el;
    }

    public static String toEL(IvyCmp info, boolean format) {
        return toELWrapper(info).toEL(format);
    }

    public static void handler(IvyCmp info) {
        if (StrUtil.isEmpty(info.getScript())) {
            info.setScript(null);
        }
        if (StrUtil.isEmpty(info.getLanguage())) {
            info.setLanguage(null);
        }
        if (StrUtil.isEmpty(info.getClazz())) {
            info.setClazz(null);
        }
        if (StrUtil.isEmpty(info.getCmpPre())) {
            info.setCmpPre(null);
        }
        if (StrUtil.isEmpty(info.getCmpFinallyOpt())) {
            info.setCmpFinallyOpt(null);
        }
        if (StrUtil.isEmpty(info.getCmpId())) {
            info.setCmpId(null);
        }
        if (StrUtil.isEmpty(info.getCmpTag())) {
            info.setCmpTag(null);
        }
        if (ArrayUtil.isEmpty(info.getCmpTo())) {
            info.setCmpTo(null);
        }
        if (StrUtil.isEmpty(info.getCmpDefaultOpt())) {
            info.setCmpDefaultOpt(null);
        }
        if (StrUtil.isEmpty(info.getCmpTrueOpt())) {
            info.setCmpTrueOpt(null);
        }
        if (StrUtil.isEmpty(info.getCmpFalseOpt())) {
            info.setCmpFalseOpt(null);
        }
        if (StrUtil.isEmpty(info.getCmpDoOpt())) {
            info.setCmpDoOpt(null);
        }
        if (StrUtil.isEmpty(info.getCmpBreakOpt())) {
            info.setCmpBreakOpt(null);
        }
        if (StrUtil.isEmpty(info.getCmpDataName())) {
            info.setCmpDataName(null);
        }
        info.setCmpData(info.getCmpData());
    }

}
